import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import { userStoreX } from "@/store/modules/user";

// const userStoreInfo: any = userStoreX()
const Home = () => import("../pages/home/index.vue");
const HomeView = () => import("../pages/HomeView.vue");
const HomeList = () => import("@/pages/home/components/dataList.vue");
const HomeButton = () => import("@/pages/home/components/button.vue");
const HomeForm = () => import("@/pages/home/components/dataForm.vue");
const Login = () => import("@/pages/login/index.vue");
const TableList = () => import("@/pages/home/components/table-list.vue");
const Xlxs = ()=>import('@/pages/xlsx.vue')

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    { path: "/login", component: Login },
    {
      path: "/index",
      name: "home",
      redirect: "/index/button",
      component: Home,
      children: [
        { path: "/index/button", name: "button", component: HomeButton },
        { path: "/index/list", name: "list", component: HomeList },
        { path: "/index/form", name: "form", component: HomeForm },
        { path: "/index/xlsx", name: "xlsx", component: Xlxs },
        {
          path: "/index/table",
          name: "table",
          component: TableList,
        },
      ],
    },

    {
      path: "/about",
      name: "about",
      component: () => import("../pages/AboutView.vue"),
    },
    {
      path: "/detail",
      name: "detail",
      component: () => import("../pages/table/index.vue"),
    },
  ],
});

router.beforeEach((to, from, next) => {
  const userStoreInfo: any = userStoreX();
  if (to.path === "/login") {
    next();
    return;
  }
  if (userStoreInfo.user.username) {
    next();
    return;
  }
  next("/login");
});

export default router;
